<?php

namespace apiend\modules\course\models;

use Yii;
use yii\data\ActiveDataProvider;
use yii\db\Exception;
use common\helpers\DateHelper;
use common\helpers\Url;
use common\models\school\School;

/**
 * This is the model class for table "{{%dynamic}}".
 *
 * @property int $id
 * @property string $user_id 用户ID
 * @property string $content
 * @property int $direction 动态类型：0主动动态，1被动动态
 * @property int $retweet_id 转发动态原始ID
 * @property int $status
 * @property string $created_at
 * @property string $updated_at
 * @property string $deleted_at
 */
class CourseSearch extends \common\models\course\Course {

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params 检索参数
     *
     * @return ActiveDataProvider
     */
    public function search($params = array()) {
        $query = static::find()
        ->alias('t')
        ->where([
            't.status' => static::STATUS_ACTIVE,
            't.audit_status' => static::AUDIT_STATUS_ACTIVE,
        ])
        ->andWhere(['>', 't.begin_time', time()]);


        $this->bindParams($query, array_filter($params));

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'pagination' => [
                'validatePage' => false
            ]
        ]);

        return $dataProvider;
    }

    /**
     * 绑定检索参数拼接
     * @param \yii\db\ActiveQuery $query
     * @param array $params 查询
     */
    protected function bindParams(&$query, $params = array()) {
        if (isset($params['id']) && $params['id']) {
            //分类检索
            $query->andFilterWhere(['t.category_id' => $params['id']]);
        }

        if (isset($params['name']) && $params['name']) {
            //名称检索
            $query->andFilterWhere(['like', 't.name', $params['name']]);
        }

        if (isset($params['area']) && $params['area']) {
            $areacode = substr($params['area'], 0, 6);
            //地区检索
            $query->joinWith('school s', true, 'INNER JOIN')
            ->andFilterWhere(['s.status' => static::STATUS_ACTIVE]);
            $distCode = intval(substr($areacode, -2));

            if ($distCode > 0) {
                $query->andFilterWhere(['s.district' => $areacode]);
            } else {
                $query->andFilterWhere(['like', 's.district', substr($areacode, 0, 4) . '%', false]);
            }
        }
        if (isset($params['time']) && $params['time']) {
            $_timeInterval = DateHelper::timeInterval($params['time']);
            if ($_timeInterval !== false) {
                $query->andFilterWhere(['between', 't.begin_time', $_timeInterval['begintime'], $_timeInterval['endtime']]);
            }
        }

        if (isset($params['sort'])) {
            if ($params['sort'] == 'last') {
                //最新上架
                $query->orderBy(['t.updated_at' => SORT_DESC]);
            } elseif ($params['sort'] == 'recent') {
                //最近开课
                $query->orderBy(['t.begin_time' => SORT_DESC]);
            } else {
                //智能排序
            }
        }
    }

    public function fields() {
        return [
            'id',
            'name',
            'thumb',
            'begintime' => function () {
                return date('Y-m-d', $this->begin_time);
            },
            'teacher' => function () {
                if ($this->teacher) {
                    return $this->teacher->name;
                }
                return '待定';
            },
            'price',
            'link' => function () {
                return Url::h5('/course/' . $this->id);
            },
        ];
    }

}
